package org.example.tmsapi.service.impl;

import org.example.tmsapi.model.ApiConfig;
import org.example.tmsapi.service.ApiConfigService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ApiConfigServiceImpl implements ApiConfigService {
    
    // 模拟数据库存储API配置数据
    private static final Map<String, ApiConfig> apiConfigMap = new HashMap<>();
    
    // 静态初始化，添加一些模拟数据
    static {
        ApiConfig apiKey = new ApiConfig();
        apiKey.setKey("API_KEY");
        apiKey.setValue("test_api_key_123456");
        apiKey.setSensitive(true);
        
        ApiConfig apiSecret = new ApiConfig();
        apiSecret.setKey("API_SECRET");
        apiSecret.setValue("test_api_secret_789012");
        apiSecret.setSensitive(true);
        
        ApiConfig baseUrl = new ApiConfig();
        baseUrl.setKey("BASE_URL");
        baseUrl.setValue("https://api.example.com");
        baseUrl.setSensitive(false);
        
        ApiConfig timeout = new ApiConfig();
        timeout.setKey("TIMEOUT");
        timeout.setValue("30000");
        timeout.setSensitive(false);
        
        apiConfigMap.put(apiKey.getKey(), apiKey);
        apiConfigMap.put(apiSecret.getKey(), apiSecret);
        apiConfigMap.put(baseUrl.getKey(), baseUrl);
        apiConfigMap.put(timeout.getKey(), timeout);
    }
    
    @Override
    public List<ApiConfig> getAllApiConfigs() {
        return new ArrayList<>(apiConfigMap.values());
    }
    
    @Override
    public ApiConfig getApiConfigByKey(String key) {
        return apiConfigMap.get(key);
    }
    
    @Override
    public boolean updateApiConfig(ApiConfig config) {
        if (config == null || config.getKey() == null || config.getKey().isEmpty()) {
            return false;
        }
        
        apiConfigMap.put(config.getKey(), config);
        return true;
    }
}